package com.algorithm;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Jack
 * @description: 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度并输出该子串。
 * @date 2021/8/31 17:16
 */
public class Solution01 {


    /**
     * 滑动窗口
     *
     * @param str
     * @return:
     * @author: jack
     * @date 2021/8/31 17:21
     */
    public int lengthOfLongestSubstring(String str) {
        Set<Character> characterSet = new HashSet<Character>();
        int n = str.length();
        int right = 0, ans = 0;
        String s = "";
        for (int i = 0; i < n; ++i) {
            if (i != 0) {
                characterSet.remove(str.charAt(i - 1));
            }
            while (right < n && !characterSet.contains(str.charAt(right))) {
                characterSet.add(str.charAt(right));
                ++right;
            }

            if ((right - i) > ans) {
                s = str.substring(i, right);

            }
            ans = Math.max(ans, right - i);
        }
        System.out.println(s);

        return ans;
    }


    public static void main(String[] args) throws Exception {
        String str = "abcdbcbbabcdefghddee";
        int length = Solution01.class.newInstance().lengthOfLongestSubstring(str);
        System.out.println(length);
    }
}
